package com.loan.web.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.loan.entity.Emp;
import com.loan.entity.ResponseResult;
import com.loan.service.EmpService;
import com.loan.util.Audience;
import com.loan.util.JwtUtil;
import com.loan.vo.EmpVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.util.List;

/**
 * 员工接口
 * @author FBF
 */
@RestController
@RequestMapping("/api/emp")
public class EmpController {
    @Autowired
    private Audience audience;
    @Autowired
    private EmpService empService;

    /**
     * 添加员工信息YJS
      * @return
     */
    @PostMapping("/add")
    public ResponseResult add(@RequestBody EmpVO empVO){
        empService.addEmp(empVO.getEmp(),empVO.getPerm());
        return ResponseResult.SUCCESS;
    }


    /**
     * 删除员工表，角色权限中间表，逻辑删除数据库状态改为“n”YJS
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public ResponseResult delete(@PathVariable int id){
        empService.deleteStatus(id);
        return ResponseResult.SUCCESS;
    }

    /**
     * 修改员工表和权限
     *
     * @param empVO
     * @return
     */
    @PostMapping("/update")
    public ResponseResult update(@RequestBody @Validated EmpVO empVO){
        empService.addEmp(empVO.getEmp(),empVO.getPerm());
        return ResponseResult.SUCCESS;
    }


    /**
     * 获取员工信息
     * @param model
     * @return
     */
    @GetMapping("/getall")
    public ResponseResult<List<Emp>> getAll(){
//        model.addAttribute("emps",empService.getAll());
//        return "redirect:/emp/list";
        return new ResponseResult<>(empService.getAll());
    }

    /**
     * FBF
     * 员工登录
     * @return
     */
    @GetMapping("/emp_login")
    public String login() {
        return "user_login";
    }

    /**
     * @FBF
     * 员工登录
     * @param account
     * @param password
     * @param session
     * @return
     *
     */
    @PostMapping("/emp_login")
    @ResponseBody
    public ResponseResult<Void> login2(String account, String password , HttpSession session) {

        QueryWrapper<Emp> queryWrapper = new QueryWrapper();
        queryWrapper.eq("account", account);
        List<Emp> list = empService.list(queryWrapper);
        String jwt = JwtUtil.createJwt(list.get(0).getId(),list.get(0).getAccount(),audience);
        if (list.size() == 0) {
            return new ResponseResult<>(501, "用户不存在");
        }
        if (!(list.get(0).getPassword().equals(password))) {
            return new ResponseResult<>(502, "密码错误");
        }
        session.setAttribute("account","你好!"+account);
        return new ResponseResult(200, "登陆成功",jwt);
    }

    /**
     *
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public ResponseResult<List<Emp>> list(){
        List<Emp> emps = empService.list();
        return new ResponseResult<>(emps);
    }
}
